import axios from 'axios'
import type { User, LoginRequest, LoginResponseData, BaseResponse, PaginationResponse } from '../types'

// 获取用户列表
export const getUsers = (page = 1, limit = 10): Promise<PaginationResponse<User>> => {
  return axios.get('/api/users', {
    params: { page, limit },
  })
}

// 获取单个用户
export const getUserById = (id: number | string): Promise<BaseResponse<User>> => {
  return axios.get(`/api/users/${id}`)
}

// 创建用户
export const createUser = (userData: Omit<User, 'id' | 'createTime'>): Promise<BaseResponse<User>> => {
  return axios.post('/api/users', userData)
}

// 更新用户
export const updateUser = (id: number | string, userData: Partial<User>): Promise<BaseResponse<User>> => {
  return axios.put(`/api/users/${id}`, userData)
}

// 删除用户
export const deleteUser = (id: number | string): Promise<BaseResponse<{ id: number | string }>> => {
  return axios.delete(`/api/users/${id}`)
}

// 用户登录
export const login = (username: string, password: string): Promise<BaseResponse<LoginResponseData>> => {
  return axios.post('/api/login', {
    username,
    password,
  } as LoginRequest)
}

// 用户登出
export const logout = (): Promise<BaseResponse<void>> => {
  return axios.post('/api/logout')
}

// 获取当前用户信息
export const getUserInfo = (): Promise<BaseResponse<User>> => {
  return axios.get('/api/user/info')
}

// 模拟错误请求
export const getError = (): Promise<BaseResponse<null>> => {
  return axios.get('/api/error')
}

// 模拟延迟请求
export const getDelay = (): Promise<BaseResponse<{ delayed: boolean }>> => {
  return axios.get('/api/delay')
}
